package com.xsz.controller;

import com.xsz.dto.JobDTO;
import com.xsz.entity.Role;
import com.xsz.entity.User;
import com.xsz.service.DTODao;
import com.xsz.service.UserService;
import com.xsz.util.ResultVOUtil;
import com.xsz.util.XSZUtil;
import com.xsz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("user")
@Api(value = "用户控制类")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    DTODao dtoDao;


    @ApiOperation(value = "登录API")
    @PostMapping("/login")
    public ResultVO selectByNamePwd(@RequestBody  User user, HttpServletRequest request) {
        System.out.println("username***"+user.getName());
        System.out.println("password***"+user.getPwd());
        String password=user.getPwd();
        String username=user.getName();
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", username);
        map.put("pwd", password);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map,httpHeaders);
        ResponseEntity<ResultVO> resultVO = restTemplate.postForEntity("http://USERCENTER/user/login", objectHttpEntity, ResultVO.class);

//        MultiValueMap<String, String> request = new LinkedMultiValueMap<>();
//        request.add("name", username);
//        request.add("pwd", password);

//        ResultVO resultVO=this.restTemplate.postForObject("http://USERCENTER/user/login",request,ResultVO.class);
        System.out.println(resultVO);
        if("成功".equals(resultVO.getBody().getMsg())){
            LinkedHashMap<String, Object> rs=(LinkedHashMap)resultVO.getBody().getData();
            HttpSession session=request.getSession();
            User loginuser =new User();
            loginuser.setId((String) rs.get("id"));
            loginuser.setName((String) rs.get("name"));
            loginuser.setPwd((String) rs.get("pwd"));
            LinkedHashMap<String, Object> roleMap=(LinkedHashMap) ((ArrayList)rs.get("roleList")).get(0);
            System.out.println(roleMap.get("role"));
            List<Role> roleList=new ArrayList<>();
            Role role=new Role();

            role.setRole((String)roleMap.get("role"));
            roleList.add(role);
            loginuser.setRoleList(roleList);
            session.setAttribute("loginuser",loginuser);

            return ResultVOUtil.success(loginuser);
        }else {
            return ResultVOUtil.error(500,resultVO.getBody().getMsg());
        }
    }

    /**
     *
     * 注册
     * 注意： 数据库保存的密码是 MD5加密以后的密文
     * @param user
     * @return
     */
    @ApiIgnore
    @PostMapping("/register")
    public ResultVO add(@RequestBody  User user) {
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        //注册用户默认是求职者
        List<Role> roleList=new ArrayList<>();
        Role role=new Role();
        role.setId(1L);
        roleList.add(role);
        user.setRoleList(roleList);
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(XSZUtil.objectToMap(user), httpHeaders);
        ResponseEntity<ResultVO> resultVO = restTemplate.postForEntity("http://USERCENTER/user/register", objectHttpEntity, ResultVO.class);
        System.out.println(resultVO);
        if("成功".equals(resultVO.getBody().getMsg())){
            return ResultVOUtil.success();
        }else {
            return ResultVOUtil.error(500,resultVO.getBody().getMsg());
        }

    }

    /**查询当前用户信息**/
    @ApiOperation(value = "当前登录用户信息")
    @GetMapping("/current")
    public ResultVO showUserInfo(HttpServletRequest req){
        User loginuser=(User)req.getSession().getAttribute("loginuser");
        return ResultVOUtil.success(loginuser);
    }

    /**查询当前用户信息**/
    @ApiOperation(value = "用户信息")
    @GetMapping("/info")
    public ResultVO showUserById(HttpServletRequest req){
        MultiValueMap<String, String> request = new LinkedMultiValueMap<>();
        String id = ((User)req.getSession().getAttribute("loginuser")).getId();

        ResultVO resultVO=this.restTemplate.getForObject("http://USERCENTER/user/info?id="+id+"",ResultVO.class);
        return ResultVOUtil.success(resultVO);
    }

    /**修改当前用户信息**/
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/infoEdit")
    public ResultVO UserInfoEdit(@RequestBody  User user, HttpServletRequest req){
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        User loginuser=(User)req.getSession().getAttribute("loginuser");
        user.setId(loginuser.getId());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(XSZUtil.objectToMap(user),httpHeaders);
        ResponseEntity<ResultVO> resultVO = restTemplate.postForEntity("http://USERCENTER/user/edit", objectHttpEntity, ResultVO.class);
        return ResultVOUtil.success(resultVO.getBody().getData());
    }

    /**验证当前用户原始密码**/
    @ApiOperation(value = "验证原始密码")
    @PostMapping("/checkPwd")
    public ResultVO checkPwd(@RequestBody  User user, HttpServletRequest req){
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        User loginuser=(User)req.getSession().getAttribute("loginuser");
        user.setId(loginuser.getId());
        user.setName(loginuser.getName());
//        System.out.println(user.getPwd());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(XSZUtil.objectToMap(user),httpHeaders);
        ResponseEntity<ResultVO> resultVO = restTemplate.postForEntity("http://USERCENTER/user/showUserByPwd", objectHttpEntity, ResultVO.class);
        return ResultVOUtil.success(resultVO.getBody().getMsg());
    }

    /**修改当前用户密码**/
    @ApiOperation(value = "修改用户密码")
    @PostMapping("/pwdEdit")
    public ResultVO pwdEdit(@RequestBody  User user, HttpServletRequest req){
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        User loginuser=(User)req.getSession().getAttribute("loginuser");
        user.setId(loginuser.getId());
        user.setName(loginuser.getName());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(XSZUtil.objectToMap(user),httpHeaders);
        ResponseEntity<ResultVO> resultVO = restTemplate.postForEntity("http://USERCENTER/user/pwdEdit", objectHttpEntity, ResultVO.class);
        return ResultVOUtil.success(resultVO.getBody().getData());
    }

    @ApiOperation(value = "根据职位id简历id投递简历")
    @PostMapping("/applyJob")
    public ResultVO selectById(@RequestBody JobDTO jobDTO){
        if(dtoDao.addPositionResume(jobDTO)>0){
            return ResultVOUtil.success();
        }else{
            return  ResultVOUtil.error(500,"申请失败");
        }

    }

}
